/**
 * Scanner.java
 *
 * Enterprise Java
 *
 * Author:  Matthew Braddy
 * Created: Oct 18, 2010
 *
 */

package java113.project.threads;

import java.util.*;
import java.util.logging.*;
import java113.project.util.DocVsSecurityException;

/**
 *
 * @author Matthew Braddy
 */
public class Scanner implements Runnable {

    private Properties properties;

    private Map ledger;

    private Logger scannerLogger;

    // a boolean used to stop the thread
    private boolean   stopping = false;

    /**
     *
     * @param processOrder
     * @param properties
     */
    public Scanner(Map ledger, Properties properties) {

        super();

        setProperties(properties);

        scannerLogger = Logger.getLogger(this.properties.getProperty("logger"));

        setProcessOrder(ledger);

    }

    /**
     *
     * @return
     */
    public Properties getProperties() {

        return this.properties;

    }

    /**
     *
     * @param properties
     */
    public void setProperties(Properties properties) {

        this.properties = properties;

    }

    /**
     *
     * @return
     */
    public Map getProcessOrder() {

        return this.ledger;

    }

    /**
     *
     * @param processOrder
     */
    public void setProcessOrder(Map ledger) {

        this.ledger = ledger;

    }

    /**
     * 
     * @return
     */
    public boolean getStopping() {

        return this.stopping;

    }

    /**
     *
     * @param stopping
     */
    public void setStopping(boolean stopping) {

        this.stopping = stopping;

    }

    /**
     *
     */
    public void run() {

        try {

            scan();
            
        } catch (DocVsSecurityException ex) {

            scannerLogger.log(Level.SEVERE, null, ex);

        }

    }

    /**
     * 
     */
    public void scan() throws DocVsSecurityException {
        
        int counter = 0;

        while (!stopping) {
            
            //Map ledger = processOrder.getOrders();

            

            //getLedger();
            
            if (ledger != null) {

                if ((ledger == null)) {
                    throw new DocVsSecurityException(
                            "File contents cannot be empty",
                            DocVsSecurityException.OUTPUT_FAIL, "strIn", ledger);

                } else if (ledger.size() > 1000000000) {
                    throw new DocVsSecurityException(
                            "File should not exceed 1Gb in size",
                            DocVsSecurityException.OUTPUT_FAIL,
                            "strIn.length()", ledger.size());
                }
                
                if (ledger.size() > counter) {
                    
                    scannerLogger.log(Level.INFO, "An order has been added.");

                    counter = ledger.size();
                    
                }                
                
            }

            try {
                
                Thread.sleep(250);

            } catch (InterruptedException ex) {
                
                scannerLogger.log(Level.SEVERE, null, ex);

            }

        }

    }

    /**
     *
     */
    public synchronized void allStop() {

        scannerLogger.log(Level.WARNING, "The Scanner's allStop()" +
                "has been called.");

        this.stopping = true;

    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {

        StringBuffer strbHold;

        strbHold = new StringBuffer();
        strbHold.append("Properties file: " + this.properties.toString());
        strbHold.append(System.getProperty("line.separator"));
        strbHold.append("ProcessOrder: " + this.ledger.toString());
        return strbHold.toString ();

    }

    

}
